package com.aaa.hospital.service.impl;

import com.aaa.hospital.mapper.EmpMapper;
import com.aaa.hospital.mapper.LoginMapper;
import com.aaa.hospital.mapper.MenuMapper;
import com.aaa.hospital.mapper.RegisterMapper;
import com.aaa.hospital.pojo.domain.*;
import com.aaa.hospital.pojo.vo.Phone;
import com.aaa.hospital.result.AjaxResult;
import com.aaa.hospital.service.EmpService;
import com.aaa.hospital.sysconst.LoginConst;
import com.aaa.hospital.util.DateUtil;
import com.aaa.hospital.util.MenuUtil;
import com.aaa.hospital.util.SendCodeUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:张海金
 * Description:T0D0
 * DateTime:2022/1/5 17:46
 **/
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private EmpMapper empMapper;
    
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RegisterMapper registerMapper;


    @Autowired
    private HttpSession session;



    @Override
    public AjaxResult login(String username, String password) {

        // 昨天时间
        String date = DateUtil.getDate();

        long update = loginMapper.upDate(date);
        // 今天时间
        String stringDate = DateUtil.getStringDate();

        long l = loginMapper.upStringDate(stringDate);

        long l1 = loginMapper.updateDate(stringDate,date);
        System.out.println(l1+"1111111111111111111111111111111111111");


        // 调用login方法并传送参数验证账号密码是否正确
        Login login = loginMapper.isLogin(username, password);

        //判断是否查询到数据，并反馈信息给用户
        if( login == null ){
            return AjaxResult.fail(LoginConst.EMP_MSG_ACCOUNT_ERROR);
        }
        if( login.getState().equals(LoginConst.EMP_STATE_UN_USE) ){
            return AjaxResult.fail(LoginConst.EMP_MSG_ACCOUNT_FREEZE);
        }
        if( login.getState().equals(LoginConst.EMP_STATE_RESIGN) ){
            return AjaxResult.fail(LoginConst.EMP_MSG_EMPLOYEE_RESIGNATION);
        }

        // 拿到员工的eid,获取账号对应的员工信息 并且存储到session中

        Integer eid = login.getEid();

        Emp emp = empMapper.getEmpbyId(eid);



        //如果查到部门状态是1,(1代表部门未启用) 反馈给用户信息
        if( emp.getPost().getState  ().equals(LoginConst.EMP_STATE_FREEZE) ){
            return AjaxResult.fail(LoginConst.EMP_MSG_DEPARTMENT_IS_NOT_ACTIVATED);
        }

        //如果部门没有被禁用的话 把员工的信息存储到session中
        session.setAttribute(LoginConst.EMP_IN_SESSION,emp);

        //如果以上都没有问题，则反馈用户登录成功
        return AjaxResult.success();
    }
    @Override
    public AjaxResult getInfo() {
        Emp emp = (Emp) session.getAttribute(LoginConst.EMP_IN_SESSION);
        HashMap<Object, Object> data = new HashMap<>();
        data.put("currentEmp",emp);

        if (emp.getRole().getState() == 0){
            if (emp.getRole().getId() == 1){
                List<Menu> superAdminMenu = menuMapper.getSuperAdminMenu();
                data.put("currentMenu",superAdminMenu);
            }else {
                List<Map<String, Object>> normalAdminMenu = menuMapper.getNormalAdminMenu(emp.getRole().getId());
                List<Map<String, Object>> haha = MenuUtil.haha(normalAdminMenu);
                data.put("currentMenu",haha);
            }
        }
        return AjaxResult.success(data);
    }

    @Override
    public AjaxResult sendCode(String telephone) {
        String code = SendCodeUtil.senCode(telephone);
        System.out.println(code);
        session.setAttribute(telephone,code);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult register(Phone p) {
//        Object attribute = session.getAttribute(p.getTelephone());
//        if (attribute == null){
//            AjaxResult.fail("短信失效");
//        }
//        if ( ! p.getCode().equals(attribute) ){
//            AjaxResult.fail("验证码不匹配");
//        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult add(Phone p) {

        p.setState(0);
//        Integer state = p.getState();

        System.out.println("注册信息=" + p);

        Object attribute = session.getAttribute(p.getTelephone());
        if( attribute == null){
            return AjaxResult.fail("短信失效");
        }
        if( ! p.getCode().equals(attribute) ){
           return AjaxResult.fail("验证码不匹配");
        }

        //查询账号是否已被注册
        String username = p.getUsername();
        String zhanghao = loginMapper.getzhanghao(username);

        System.out.println("已查到的账号信息" + zhanghao);

        if( zhanghao == username ){
            return AjaxResult.fail("此账号已被注册");
        }

        if( zhanghao == null ){
            int add = registerMapper.add(p);
            if ( add > 0 ){
                return AjaxResult.success();
            }
        }
        return AjaxResult.fail("此账号已被注册");
    }

    @Override
    public AjaxResult updatePassword(@Param("password") String password, @Param("newpassword") String newpassword) {
        Emp attribute = (Emp) session.getAttribute(LoginConst.EMP_IN_SESSION);
        Integer id = attribute.getId();
        int i = loginMapper.updatepassword(id, password, newpassword);
        if (i>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.fail();
        }
    }

    @Override
    public AjaxResult signin(String zhanghao, String mima) {

      Signin signin =  loginMapper.signin(zhanghao, mima);

       if( signin == null ){
           return AjaxResult.fail(LoginConst.EMP_MSG_ACCOUNT_ERROR);
       }
       if ( signin.getState().equals(LoginConst.EMP_STATE_FREEZE)){
           return AjaxResult.fail(LoginConst.EMP_MSG_ACCOUNT_FREEZE);
       }
       if ( signin.getState() == 2 ){
           return AjaxResult.fail("对不起，您已注销");
       }

       session.setAttribute("signin",signin);

        Integer id = signin.getId();

        Signin user = empMapper.getUser(id);

        return AjaxResult.success(user);
    }

    //验证是否登录以及拿到用户信息展示
    @Override
    public AjaxResult yanzheng() {
        Object signin = session.getAttribute("signin");
        System.out.println("验证用户是否登录" + signin);
        if( signin == null){
            return AjaxResult.fail("您还没有登录，请先登录");
        }else {
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult verification() {
        Signin signin  = (Signin) session.getAttribute("signin");
        if ( signin == null ){
            return AjaxResult.fail("请先登录再查看预约信息");
        }else{
            String zhanghao = signin.getZhanghao();
            return AjaxResult.success(zhanghao);
        }
    }

//    实现注销
    @Override
    public AjaxResult logout() {
        Emp attribute = (Emp) session.getAttribute(LoginConst.EMP_IN_SESSION);
        System.out.println(attribute);
        Integer ger = attribute.getId();
        System.out.println(ger);
        int i = loginMapper.logout(ger);
        if (i > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.fail();
        }
    }

    @Override
    public AjaxResult getNickname() {
        Signin signin = (Signin) session.getAttribute("signin");
        String nickname = signin.getNickname();
        System.out.println("拿到昵称展示" + nickname);
        return AjaxResult.success(nickname);

    }

}
